/***
 * * PROJECT
 * *    Logical Neighborhood Routing
 * * VERSION
 * *    $Revision: 1.5 $
 * * DATE
 * *    $Date: 2007/01/29 17:48:01 $
 * * LOG
 * *    $Log: SetMembershipPredicate.java,v $
 * *    Revision 1.5  2007/01/29 17:48:01  lmottola
 * *    Added debug info in toString() method.
 * *
 * *    Revision 1.4  2006/11/03 23:09:34  lmottola
 * *    Changed nodeAttributes package.
 * *
 * *    Revision 1.3  2006/11/03 23:08:17  lmottola
 * *    Traffic application beta running.
 * *
 * *    Revision 1.2  2006/10/24 19:20:22  lmottola
 * *    Added dedicated scope definition (LNScope) instead of ScopedChannel.
 * *
 * *    Revision 1.1  2006/10/24 19:00:59  lmottola
 * *    Added attributes and predicates.
 * *
 * *
 * *    Revision 1.2  2006/10/19 21:21:52  lmottola
 * *    Added CVS preamble.
 * *
 * *
 * *   Logical Neighborhoods Routing - Java Version
 * *                                
 * *
 * *   This program is free software; you can redistribute it and/or
 * *   modify it under the terms of the GNU General Public License
 * *   as published by the Free Software Foundation; either version 2
 * *   of the License, or (at your option) any later version.
 * *
 * *   This program is distributed in the hope that it will be useful,
 * *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 * *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * *   GNU General Public License for more details.
 * *
 * *   You should have received a copy of the GNU General Public License
 * *   along with this program; if not, you may find a copy at the FSF web
 * *   site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
 * *   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * *   Boston, MA  02111-1307, USA
 ***/

package atag.runtime.ln.predicates;

import java.util.HashSet;
import java.util.Set;

import atag.runtime.ln.nodeAttributes.NodeAttribute;
import atag.runtime.ln.nodeAttributes.SetAttribute;

public class SetMembershipPredicate implements LNPredicate {

	// Possible operators
	public static final int IS_IN = 0;

	public static final int IS_NOT_IN = 1;

	private String attrName;

	private int operator;

	private Object predicateValue;

	public SetMembershipPredicate(Object predicateValue, int operator,
			String attrName) {

		this.attrName = attrName;
		this.operator = operator;
		this.predicateValue = predicateValue;
	}

	public boolean matches(NodeAttribute attr) {

		if (!(attr instanceof SetAttribute))
			return false;

		if (!attr.getKey().equals(attrName))
			return false;

		HashSet attrValue = new HashSet((Set) attr.getValue());

		switch (operator) {

		case IS_IN:
			return attrValue.contains(predicateValue);

		case IS_NOT_IN:
			return !attrValue.contains(predicateValue);

		default:
			return false;
		}
	}
	
	public String toString() {

		String ret = "SetMembership predicate for predicateValue " + predicateValue;
		
		switch (operator) {
		case IS_IN:
			ret = ret.concat(" IS IN ");
			break;

		case IS_NOT_IN:
			ret = ret.concat(" IS NOT IN ");
			break;
		}
		
		ret = ret.concat(attrName);
		
		return ret;
	}
}
